Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

148
Visualizações
I'm getting this error: expected [ '' ] to deeply equal [], but it works fine on vs code

This was the question from codewars:

Complete the solution so that it splits the string into pairs of two characters. If the string contains an odd number of characters then it should replace the missing second character of the final pair with an underscore ('_').

Examples:

  • 'abc' => ['ab', 'c_']
  • 'abcdef' => ['ab', 'cd', 'ef']

And this is what i have achieved:

function solution(str) {
      const letters = str.split('');
      let text = '';
      for (const letter of letters) {
        text += letter;
        if (text.replace(/ /g, '').length % 2 === 0)
          letter !== letters[letters.length - 1] && (text += ' ');
      }
      if (str.length % 2 !== 0) text += '_';
      return text.split(' ');
    }
    
    console.log(solution('abcd'));

This is the error, i'm getting: expected [ '' ] to deeply equal []

A screenshot from codewars

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

function solution(str) {
  const letters = str.split('');
  let text = '';
  for (const letter of letters) {
    text += letter;
    if (text.replace(/ /g, '').length % 2 === 0)
      letter !== letters[letters.length - 1] && (text += ' ');
  }
  if (str.length % 2 !== 0) text += '_';
  console.log(text);
  return (text === '' && []) || text.split(' ');
}

console.log(solution(''));

Now its working as expected when solution function is called as solution(' ') and now it returns an empty array [ ] instead of an array with an empty string[' ']

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda